package com.cml.dm.admin.service.handler;

import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cml.dm.admin.entity.DmExeLogger;
import com.cml.dm.admin.entity.DmMetadata;
import com.cml.dm.admin.mapper.DmExeLoggerMapper;
import com.cml.dm.admin.mapper.DynamicSqlMapper;
import com.cml.dm.admin.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import sun.nio.ch.DirectBuffer;

import java.io.File;
import java.io.FileInputStream;
import java.nio.ByteBuffer;
import java.util.*;

public class RecoveryTaskHandler extends TaskHandler{

    private SqlGenerateServiceFactory sqlGenerateServiceFactory;

    private FileCompressFactory fileCompressFactory;

    private EncryptionFactory encryptionFactory;

    private DynamicSqlMapper dynamicSqlMapper;

    public RecoveryTaskHandler(){
        this.sqlGenerateServiceFactory = SpringUtil.getBean(SqlGenerateServiceFactory.class);
        this.fileCompressFactory = SpringUtil.getBean(FileCompressFactory.class);
        this.encryptionFactory = SpringUtil.getBean(EncryptionFactory.class);
        this.dynamicSqlMapper = SpringUtil.getBean(DynamicSqlMapper.class);
    }

    @Override
    public void execute(DmMetadata dmMetadata, DmExeLogger dmExeLogger, Object extParameter) {
        dmExeLogger.setExeType(dmExeLogger.recovery());
        DmMetadata curMetadata = dmMetadata;
        do {
            List<String> sqls = sqlGenerateServiceFactory.getSqlGenerateService(curMetadata,dmExeLogger,extParameter).sqlGenerate(curMetadata,dmExeLogger,extParameter);
            //TODO 执行sql
            for(String sql : sqls){
                this.dynamicSqlMapper.update(sql);
            }
            curMetadata = curMetadata.getNextDmMetadata();
        }while (null != curMetadata);
    }
}
